package client.comm.baoding.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.ui.viewmodel.MiddleVm
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.network.data.Ret
import client.comm.commlib.out_time
import client.comm.commlib.toast.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import retrofit2.Response

class Zz2ViewModel(app: Application) : MiddleVm(app) {
    val call = MutableLiveData<JsonResult<Ret>>()
    val isShowing = MutableLiveData<Boolean>()
    val username = MutableLiveData<String>("")
    val zzNum = MutableLiveData<String>()
    val zzType = MutableLiveData<Int>(-1)

    val userInfo = MutableLiveData<UserInfo>()

    fun checkYuE() {
        zzType.value = 1
    }

    fun checkIntegral() {
        zzType.value = 2
    }


    /**
     */
    fun ok(it: String) {
        isShowing.value = true
        viewModelScope.launch {
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }

                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.transfer(
                                username.value!!,
                                zzNum.value!!
                            )
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.value = false
                }.catch { cause ->
                    if (cause != null) {
                        toast(cause.message)
                    }
                }.collectLatest {
                    call.value = it?.body()

                }
        }
    }

    fun okWithSys(it: String) {
        isShowing.value = true
        viewModelScope.launch {
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }

                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.user_income_transfer(
                                zzNum.value!!
                            )
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.value = false
                }.catch { cause ->
                    if (cause != null) {
                        toast(cause.message)
                    }
                }.collectLatest {
                    call.value = it?.body()

                }
        }
    }


}